package service;

import java.sql.*;

public class DB {
  private static DB db;

  static {
    try {
      db = new DB();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  public static DB getDb() {
    return db;
  }

  private Statement stmt;

  private DB() throws ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    try {
      String url = "jdbc:mysql://localhost:3306/birthday"
        + "?user=root&password=root";
      Connection con = DriverManager.getConnection(url);
      stmt = con.createStatement();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public Integer signIn(String uid, String pw) throws SQLException {
    String query = String.format("SELECT 1 from user WHERE uid = '%s' and pw = '%s'", uid, pw);
    System.out.println(query);
    ResultSet rs = stmt.executeQuery(query);
    rs.last();
    return rs.getRow();
  }

  public void signUp(String uid, String pw, String uname) throws SQLException {
    String query = String.format("INSERT INTO user (uid, pw, uname) VALUES('%s', '%s', '%s')", uid, pw, uname);
    System.out.println(query);
    stmt.executeUpdate(query);
  }
}
